package com.meihua.mes2ehr.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meihua.mes2ehr.entity.FsMesPointTarget;
import com.meihua.mes2ehr.entity.MesInfData;
import com.meihua.mes2ehr.service.FsMesPointTargetService;
import com.meihua.mes2ehr.service.MesInfDataService;
import com.meihua.mes2ehr.mapper.MesInfDataMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
* @author maheqiang
* @description 针对表【mes_inf_data】的数据库操作Service实现
* @createDate 2024-04-21 13:48:52
*/
@Service
public class MesInfDataServiceImpl extends ServiceImpl<MesInfDataMapper, MesInfData>
    implements MesInfDataService{

    public  FsMesPointTargetService fsMesPointTargetService;


    @Override
    public void getInfMesMessage(String pointNo,String queryTime) {
//        System.out.println(pointNo);
//        System.out.println(queryTime);
//        this.mesInfDataService.remove(null);
//            FsMesPointTarget fsMesPointTarget = this.fsMesPointTargetService.getById(271);
//            String mesPointNo = "LF01_PH_DG1，LF01_PH_DG2，LF01_PH_23_1，LF01_LT_DG1Z，LF01_LT_DG1，LF01_LT_DG2Z，LF01_LT_DG2，LF01_LT_23Z，LF01_LT_23_1";
//            String[] pointArr = pointNo.split("，");
//            final String[] s = {""};
//            Stream.of(pointArr).forEach(item -> {
//                s[0] += "BCMH_" + item.substring(0, 5) + "W5." + item.substring(5) + ',';
//            });
//
//        pointNo = s[0].substring(0, s[0].length() - 1);
//        System.out.println(mesPointNo);

//            for (int ii = 1; ii <= 31; ii++) {
//                StringBuilder queryTime = new StringBuilder();
            List<MesInfData> mesInfDataList = new ArrayList<>();
//                for (int j = 0; j < 24; j += 2) {
//                    queryTime.append("2024-03-").append(queryDate).append("%20").append(j).append(":00:00,");
//                }
//                queryTime = "2024-03-" + ii + "%20" + j + ":00:00";
//                queryTime = new StringBuilder(queryTime.substring(0, queryTime.length() - 1));
                String urlString = "http://172.18.1.60:8085/Api/RmisFine/GetHistoryData?Tags=" + pointNo + "&QueryTimes=" + queryTime;
                String result1 = HttpUtil.get(urlString);

                result1 = result1.substring(14);
                String[] split = result1.split("[,\\n]");
                for (int i = 0; i < split.length; i += 4) {
                    MesInfData mesInfData = new MesInfData();
                    mesInfData.setPointNo(split[i]);
                    mesInfData.setValue(split[i + 1]);
                    mesInfData.setQueryTime(DateUtil.parse(StrUtil.isBlank(split[i + 2]) ? null : split[i + 2]).toLocalDateTime());
                    mesInfData.setRemark(StrUtil.isBlank(split[i + 3]) ? null : split[i + 3]);
//                    mesInfData.setTargetId(fsMesPointTarget.getTargetId());
                    mesInfDataList.add(mesInfData);
                }
//        System.out.println(mesInfDataList);
            this.saveBatch(mesInfDataList);

    }


}




